package com.neu.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.base.Result;
import com.neu.entity.FpDIValue;
import com.neu.mapper.FpDIValueMapper;
import com.neu.service.FpDIValueService;
import org.springframework.stereotype.Service;
import com.neu.entity.dto.DIDTO;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map;
import java.util.List;

/**
 * FpDIValue 服务实现类
 */
@Service
public class FpDIValueServiceImpl extends ServiceImpl<FpDIValueMapper, FpDIValue> implements FpDIValueService {
        @Autowired
        private FpDIValueMapper fpDIValueMapper; // 注入 MyBatis-Plus 的 Mapper

        @Override
        public Result saveDI(DIDTO diDTO) {
            String taskId = diDTO.getTaskId();
            Map<String, Integer> diDictionary = diDTO.getDiDictionary(); // 获取字典形式的 DI 数据
            // 将字典转换为实体对象
            FpDIValue diValue = new FpDIValue();
            diValue.setTaskId(taskId);
            diValue.setFir(diDictionary.get("fir"));
            diValue.setSec(diDictionary.get("sec"));
            diValue.setThi(diDictionary.get("thi"));
            diValue.setFou(diDictionary.get("fou"));
            diValue.setFif(diDictionary.get("fif"));
            diValue.setSix(diDictionary.get("six"));
            diValue.setSen(diDictionary.get("sev"));
            diValue.setEig(diDictionary.get("eie"));
            diValue.setNin(diDictionary.get("nin"));
            diValue.setTen(diDictionary.get("ten"));
            diValue.setEle(diDictionary.get("ele"));
            diValue.setTwe(diDictionary.get("twe"));
            diValue.setThirteenth(diDictionary.get("thirteenth"));
            diValue.setFourteenth(diDictionary.get("fourteenth"));
            int sum=0;
            sum+=diValue.getFir()+diValue.getSec()+diValue.getThi()+diValue.getFou()+diValue.getFif()+diValue.getSix()+diValue.getSen()+diValue.getEig()+diValue.getNin()+diValue.getTen()+diValue.getEle()+diValue.getTwe()+diValue.getThirteenth()+diValue.getFourteenth();
            double vaf=sum*0.01+0.65;
            // 保存到数据库
            fpDIValueMapper.insert(diValue); // 使用 MyBatis-Plus 提供的 insert 方法保存记录
            return Result.success(vaf);
        }



    /**
     * 根据 taskId 查询 DI 数据（返回 DTO）
     */
    @Override
    public DIDTO getDIByTaskId(String taskId) {
        // 使用 LambdaQueryWrapper 查询数据
        LambdaQueryWrapper<FpDIValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FpDIValue::getTaskId, taskId);

        List<FpDIValue> diList = this.list(queryWrapper);

        // 封装为 DTO 返回
        DIDTO diDTO = new DIDTO();
        return diDTO;
    }

    /**
     * 根据 taskId 查询 DI 数据（返回实体列表）
     */
    @Override
    public List<FpDIValue> getByTaskId(String taskId) {
        // 使用 LambdaQueryWrapper 查询数据
        LambdaQueryWrapper<FpDIValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FpDIValue::getTaskId, taskId);
        return this.list(queryWrapper);
    }
}
